<?php
set_time_limit(0);
ini_set('memory_limit', '32M');

include 'constants.php';
include 'class_data_file_list.php';
include 'class_cities_spreadsheet_reader.php';
include 'class_database_connection.php';
include 'class_cities_table_creator.php';
include 'class_cities_table_populator.php';

$database_connection = new Database_Connection(DATABASE_HOST, DATABASE_USER, DATABASE_PASS, DATABASE_NAME);
$connected = $database_connection->connect();
if (! $connected) {
    echo 'Erro ao conectar no banco de dados. Verifique o arquivo constants.php';
    die;
}
$connection = $database_connection->connection;

$table_creator = new Cities_Table_Creator(CITIES_TABLE_NAME, ESTATE_NAME_COLUMN_NAME, CITY_NAME_COLUMN_NAME, ID_NAME_COLUMN_NAME);
$table_created = $table_creator->create_cities_table($connection);
if (! $table_created) {
    echo 'Erro ao criar a tabela. Verifique o arquivo constants.php';
    die;
}




$data_file_list = new Data_File_List('data/');
$data_file_list->get_file_paths_list();
$list_of_files =$data_file_list->files_paths_list;

$table_populator = new Cities_Table_Populator(CITIES_TABLE_NAME, ESTATE_NAME_COLUMN_NAME, CITY_NAME_COLUMN_NAME);

foreach($list_of_files as $file_path) {
    $spreadsheet_reader = new Cities_SpreadSheet_Reader($file_path);
    $spreadsheet_reader->load_spreadsheet();
    $table_populator->estate_name = $spreadsheet_reader->get_state_acronym();
    $table_populator->cities = $spreadsheet_reader->get_cities_names();
    $table_populator->populate_cities_table($connection);
}
echo 'Terminado';


?>
